package com.zhousb.deviceManager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhousb.deviceManager.common.PageRequest;
import com.zhousb.deviceManager.common.R;
import com.zhousb.deviceManager.common.ResponseResult;
import com.zhousb.deviceManager.model.bo.ExportCheckedBo;
import com.zhousb.deviceManager.model.bo.OrderBO;
import com.zhousb.deviceManager.model.so.OrderSo;
import com.zhousb.deviceManager.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/order")
@Tag(name = "产品清单")
public class OrderController {


    @Resource
    private OrderService orderService;
    @PostMapping("/page")
    @Operation(summary = "产品列表")
    public ResponseResult page(@RequestBody PageRequest<OrderSo> so) {
        return R.success(orderService.page(new Page<>(so.getCurrent(), so.getSize()), so.getQuery()));
    }

    @PostMapping("/create")
    @Operation(summary = "下单")
    public ResponseResult create(@RequestBody OrderBO bo) {
        return R.success(orderService.create(bo));
    }

    @PostMapping("/update")
    @Operation(summary = "修改订单")
    public ResponseResult update(@RequestBody OrderBO bo) {
        return R.success(orderService.update(bo));
    }

    @DeleteMapping("/cancel")
    @Operation(summary = "撤销订单")
    public ResponseResult update(@Param("orderId")String orderId) {
        return R.success(orderService.cancel(orderId));
    }

    @GetMapping("/lock")
    @Operation(summary = "锁定订单")
    public ResponseResult lock(@Param("orderId")String orderId) {
        return R.success(orderService.lock(orderId));
    }

    @GetMapping("/unlock")
    @Operation(summary = "取消锁定订单")
    public ResponseResult unlock(@Param("orderId")String orderId) {
        return R.success(orderService.unlock(orderId));
    }

    @GetMapping("/deliver")
    @Operation(summary = "订单发货")
    public ResponseResult deliver(@Param("orderId")String orderId) {
        return R.success(orderService.deliver(orderId));
    }

    @GetMapping("/unDeliver")
    @Operation(summary = "订单取消发货")
    public ResponseResult unDeliver(@Param("orderId")String orderId) {
        return R.success(orderService.unDeliver(orderId));
    }

    @GetMapping("/getAllCreateOrderId")
    @Operation(summary = "订单取消发货")
    public ResponseResult getAllCreateOrderId() {
        return R.success(orderService.getAllCreateOrderId());
    }

    @PostMapping("/exportCheckedOrder")
    @Operation(summary = "")
    public void exportCheckedOrder(@RequestBody ExportCheckedBo bo, HttpServletResponse response) throws IOException {
        orderService.exportCheckedOrder(bo,response);
    }

    @GetMapping("/logByProduct")
    @Operation(summary = "产品订单明细")
    public ResponseResult logByProduct(@RequestParam("productId")String productId,
                                       @RequestParam("orderStatus")String orderStatus,
                                       @RequestParam(value = "startTime",required = false)String startTime,
                                       @RequestParam(value = "endTime",required = false)String endTime
                                       ) {
        return R.success(orderService.logByProduct(productId,orderStatus,startTime,endTime));
    }

    @GetMapping("/exportLogByProduct")
    @Operation(summary = "产品订单明细")
    public void exportLogByProduct(@RequestParam("productId")String productId,
                                       @RequestParam("orderStatus")String orderStatus,
                                       @RequestParam(value = "startTime",required = false)String startTime,
                                       @RequestParam(value = "endTime",required = false)String endTime,
                                             HttpServletResponse response
    ) throws IOException {
        orderService.exportLogByProduct(productId,orderStatus,startTime,endTime,response);
    }
}
